package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.*;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

// 菜品管理
@Service
@Transactional
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    //分页和条件查询
    @Override
    public Page<Dish> findByPage(Integer pageNum, Integer pageSize, String name) {
        // 1.构建菜品条件
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper();
        lqw.like(StrUtil.isNotEmpty(name), Dish::getName, name);//这三个参数执行流程
        // 2.构建分页
        Page<Dish> page = new Page<>(pageNum, pageSize);
        // 3.查询分页
        page = dishMapper.selectPage(page, lqw);
        // 4.遍历菜品list集合
        List<Dish> records = page.getRecords();
        for (Dish dish : records) {
            // 4-1 查询口味
            // a) 构建口味条件
            LambdaQueryWrapper<DishFlavor> ld = new LambdaQueryWrapper<>();
            ld.eq(DishFlavor::getDishId, dish.getId());
            // b）查询口味列表
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(ld);
            // c）封装到菜品实体中
            dish.setFlavors(dishFlavors);
            // 4-2 查询分类
            Category category = categoryMapper.selectById(dish.getCategoryId());
            dish.setCategoryName(category.getName());
        }
        // 5.返回分页对象
        return page;
    }
    //新增菜品信息
    @Override
    public void save(Dish dish) {
        // 1.直接保存菜品基本信息
        log.info("新增之前id：{}",dish.getId());
        dishMapper.insert(dish);
        log.info("新增之后id：{}",dish.getId());
        // 2.获取并遍历口味
        List<DishFlavor> flavors = dish.getFlavors();//
        for (DishFlavor flavor : flavors) {
            // 2-1 关联菜品id
            flavor.setDishId(dish.getId());
            // 2-2 保存数据库
            dishFlavorMapper.insert(flavor);
        }
    }
    //修改回显菜品
    @Override
    public Dish findById(Long id) {
        //根据id查询菜品信息
        Dish dish = dishMapper.selectById(id);
        //根据id查询菜品口味列表
        // 2-1 构建条件
        LambdaQueryWrapper<DishFlavor> lw=new LambdaQueryWrapper<>();
        lw.eq(DishFlavor::getDishId,id);
        //执行查询
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(lw);
        //将口味列表设置到菜品中
        dish.setFlavors(dishFlavors);
        //返回菜品
        return dish;

    }
    //修改菜品
    @Override
    public void update(Dish dish) {
        //调用mapper
        dishMapper.updateById(dish);
        //先删除旧口味
        LambdaQueryWrapper<DishFlavor> lw=new LambdaQueryWrapper<>();
        lw.eq(DishFlavor::getDishId,dish.getId());
        // 根据条件删除
        dishFlavorMapper.delete(lw);
        // 3.遍历新口味
        List<DishFlavor> dishFlavors = dish.getFlavors();
        for (DishFlavor dishFlavor : dishFlavors) {
            // 3-1 关联菜品id
            dishFlavor.setDishId(dish.getId());
            // 3-2 保存数据库
            dishFlavorMapper.insert(dishFlavor);
        }
    }

    // 启停售    批量修改 update dish set status = 0|1 where id in(xxx,xxx,xxx)
    @Override
    public void updateStatus(Integer type, List<Long> ids) {
        // 构建条件
        LambdaQueryWrapper<Dish> lw=new LambdaQueryWrapper<>();
        lw.in(Dish::getId,ids);// where id in(xxx,xxx,xxx)
        // 构建修改实体
        Dish dish=new Dish();
        dish.setStatus(type);
        // 执行数据库
        dishMapper.update(dish,lw);
    }

    @Override
    public void deleteBatchIds(List<Long> ids) {
        // 1.先判断菜品是否处于停售状态  select count(*) from dish where status = 1 and  id in (xx,xx,xxx)
        LambdaQueryWrapper<Dish> lw=new LambdaQueryWrapper<>();
        lw.in(Dish::getId,ids);// id in (xx,xx,xxx)
        lw.eq(Dish::getStatus,1);// status = 1
        Integer integer = dishMapper.selectCount(lw);
        //判断
        if(integer>0){
            throw new CustomException("删除中包含未停售的菜品");
        }
        // 2.删除菜品  delete from dish where id in (xx,xx,xxx)
        dishMapper.deleteBatchIds(ids);
        // 3.删除口味 delete from dish_flavor where dish_id in (xx,xx,xxx)
        LambdaQueryWrapper<DishFlavor> ld=new LambdaQueryWrapper<>();
        ld.in(DishFlavor::getDishId,ids);
        dishFlavorMapper.delete(ld);
    }
    //新增回显
    @Override
    public List<Dish> findAll( Long id,String name ) {
        //构建条件
        LambdaQueryWrapper<Dish> lq=new LambdaQueryWrapper<>();
        lq.eq(id != null,Dish::getCategoryId,id);
        lq.like(StrUtil.isNotEmpty(name),Dish::getName,name);
        //mapper查询
        List<Dish> dish = dishMapper.selectList(lq);
        return dish;

    }
    // 根据分类id+状态查询菜品列表
    @Override
    public List<Dish> findList(Long category, Integer status) {
        // 1-1 构建条件  select * from dish where category_id = ? and status = ?
        //构建条件
        LambdaQueryWrapper<Dish> lq=new LambdaQueryWrapper<>();
        lq.eq(Dish::getStatus,status);
        lq.eq(Dish::getCategoryId,category);
        //调用mapper查询
        List<Dish> dishList = dishMapper.selectList(lq);
        // 2.遍历菜品 //将菜品和口味关联
        for (Dish dish : dishList) {
            //构建条件
            LambdaQueryWrapper<DishFlavor> qw=new LambdaQueryWrapper<>();
            qw.eq(DishFlavor::getDishId,dish.getId());
            //调用mapper查询
            List<DishFlavor> dishFlavors = dishFlavorMapper.selectList(qw);
            //将查询出来的口味与菜品关联
            dish.setFlavors(dishFlavors);
        }
            //返回给前端

        return dishList;
    }


}
